package cn.lijunkui.controller;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import com.auth0.jwt.JWT;
import com.auth0.jwt.interfaces.DecodedJWT;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.google.common.base.Objects;
import cn.lijunkui.exception.message.CodeEnum;
import cn.lijunkui.exception.message.ReturnMessage;
import cn.lijunkui.exception.message.ReturnMessageUtil;
import cn.lijunkui.utils.JWTService;

@RestController
public class LoginController {
    Logger log = LoggerFactory.getLogger(LoginController.class);
    @Autowired
    private JWTService jwtService;

    @RequestMapping("/login")
    public ReturnMessage<Object> login(String loginName, String password, HttpServletRequest request) {
        if (valid(loginName, password)) {
            ReturnMessageUtil.error(CodeEnum.LOGINNAMEANDPWDERROR);
        }

        Map<String, String> userInfo = createUserInfoMap(loginName, password);
        String token = jwtService.createToken(userInfo, 1);

        ServletContext context = request.getServletContext();
        context.setAttribute(token, token);
        log.info("token:" + token);

        DecodedJWT jwt = JWT.decode(token);
        System.out.println(jwt.getClaim("uid").asString());
        System.out.println(jwt.getClaim("username").asString());
        System.out.println(jwt.getClaim("email").asString());
        System.out.println(jwt.getClaim("avatar").asString());
        System.out.println(jwt.getClaim("phone").asString());
        System.out.println(jwt.getClaim("birth").asString());
        System.out.println(jwt.getClaim("sex").asString());
        return ReturnMessageUtil.sucess(token);
    }

    @GetMapping("/logout")
    public ReturnMessage<?> logout(String token, String issuer, HttpServletRequest request) {
        ServletContext context = request.getServletContext();
        context.removeAttribute(token);
        return ReturnMessageUtil.sucess();
    }


    private Map<String, String> createUserInfoMap(String loginName, String password) {
        Map<String, String> userInfo = new HashMap<>();
        userInfo.put("uid", "uid");
        userInfo.put("username", "username");
        userInfo.put("email", "email");
        userInfo.put("avatar", "avatar");
        userInfo.put("nickname", "nickname");
        userInfo.put("phone", "phone");
        userInfo.put("birth", "birth");
        userInfo.put("sex", "sex");
        return userInfo;
    }

    private boolean valid(String loginName, String password) {
        if (Objects.equal("ljk", loginName) && Objects.equal("123456", password)) {
            return true;
        }
        return false;
    }


} 
